/*
 * Menu: ActionScript > Generate Accessors
 * Key: M3+1
 * DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript
 */
 
function main() 
{
  var editor = editors.activeEditor
  var source = editor.source
  
  if (editor.selectionRange)
    var range  = editor.selectionRange
  var offset = range.startingOffset
  
  var text = source.substring (offset, range.endingOffset)
  var propDefs = text.match(/(\w+:\w+)/g);
  
  // var output = "";
  var props = [];
  var accessors = [];
  
  for( var i = 0; i < propDefs.length; i++) {
	  var parts = propDefs[i].split(':');
	  var name = parts[0];
	  var type = parts[1]
	  props[i] = "\t\tprivate var _" + name + ':' + type + ';\n';
	  accessors[i] = "\n\n\t\tpublic function get " + name + "():" + type + " {" +
	                 "\n\t\t\treturn _" + name + ';' +
	                 "\n\t\t}" +
	                 "\n\n\t\tpublic function set " + name + "(" + name + ":" + type + "):void {" +
	                 "\n\t\t\t_" + name + " = " + name + ";" +
	                 "\n\t\t}";
  }
  
  var output = "";
  for (var i = 0; i < props.length; i++) {
	  output += props[i]
  }
  output = output.substring(0, output.length-1);
  for (var i = 0; i < accessors.length; i++) {
	  output += accessors[i];
  }
  
  editor.applyEdit(offset, range.endingOffset - offset, output);
}

function debug(s) {
  Packages.org.eclipse.jface.dialogs.MessageDialog.openInformation(window.getShell(), "Monkey Debugging", s);
}